﻿using FairyGUI;

/// <summary>
/// UI根控制器
/// </summary>
public class UIRootController : BaseController {

    /// <summary>
    /// 设计宽度
    /// </summary>
    public int _designWidth;

    /// <summary>
    /// 设计高度
    /// </summary>
    public int _designHeight;

    /// <summary>
    /// 当前UI界面
    /// </summary>
    UIBaseWindow _curUI;
    /// <summary>
    /// 网络掩码
    /// </summary>
    GComponent _netMask;
    /// <summary>
    /// 当前顶
    /// </summary>
    int _curTop;

    public UIBaseWindow curUI { get { return _curUI; } }

    static UIRootController _inst;
    public static UIRootController inst { get { return _inst; } }

    protected override void InitData()
    {
        _curTop = 1;

        _inst = this;
    }

    protected override void InitView()
    {
        GRoot.inst.SetContentScaleFactor(_designWidth, _designHeight);

        UIPackage.AddPackage("UI/网络掩码");
        _netMask = UIPackage.CreateObject("网络掩码", "网络掩码").asCom;
        GRoot.inst.AddChild(_netMask);

        GameController.inst.OnUIReady();
    }

    protected override void DestroyView()
    {
        base.DestroyView();

        UIPackage.RemovePackage("UI/网络掩码");
    }

    public void SwitchTo(UIBaseWindow window)
    {
        if (_curUI != null)
        {
            GRoot.inst.RemoveChild(_curUI.uiComp);
            _curUI.OnExit();
        }

        _curUI = window;
        if (_curUI != null)
        {
            GRoot.inst.AddChildAt(_curUI.uiComp, 0);
        }
    }

    /// <summary>
    /// 显示弹出窗口
    /// </summary>
    /// <param name="window"></param>
    public void PopupWindow(UIPopupWindow window)
    {
        window.OnClose(() => {
            _curTop -= 1;
        });
        GRoot.inst.AddChildAt(window.uiComp, _curTop);
        _curTop += 1;
    }

    public void ShowMask(bool show)
    {
        _netMask.visible = show;
        _netMask.enabled = show;
    }
}
